package cn.schoolwow.quickflow;

import cn.schoolwow.quickflow.domain.FlowConfig;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.quickflow.flow.CompositeBusinessFlow;
import cn.schoolwow.quickflow.service.quickFlow.QuickFlowInvocationHandler;
import com.alibaba.fastjson.parser.ParserConfig;

import java.lang.reflect.Proxy;

public class QuickFlowBuilder {
    static{
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }
    private static QuickFlow defaultQuickFlow = newInstance();

    public static QuickFlow newInstance() {
        FlowConfig flowConfig = new FlowConfig();
        QuickFlowInvocationHandler quickFlowInvocationHandler = new QuickFlowInvocationHandler(flowConfig);
        QuickFlow quickFlowProxy = (QuickFlow) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class<?>[]{QuickFlow.class}, quickFlowInvocationHandler);
        flowConfig.quickFlow = quickFlowProxy;
        return quickFlowProxy;
    }

    /**获取默认QuickFlow对象*/
    public static QuickFlow quickFlow(){
        return defaultQuickFlow;
    }

    /**执行静态业务逻辑(仅执行一次)*/
    public static QuickFlow executeStaticFlowList(BusinessFlow... businessFlows){
        defaultQuickFlow.executeFlow(businessFlows);
        return defaultQuickFlow;
    }

    public static QuickFlowExecutor startFlow(BusinessFlow businessFlow){
        return defaultQuickFlow.startFlow(businessFlow);
    }

    public static QuickFlowExecutor startFlow(CompositeBusinessFlow compositeBusinessFlow){
        return defaultQuickFlow.startFlow(compositeBusinessFlow);
    }

    public static FlowContext executeFlow(BusinessFlow businessFlow){
        return defaultQuickFlow.executeFlow(businessFlow);
    }

    public static FlowContext executeFlow(CompositeBusinessFlow compositeBusinessFlow){
        return defaultQuickFlow.executeFlow(compositeBusinessFlow);
    }
}
